<template>
  <!--ASN明细-->
  <div id="AsnDetails">
    <g-details :detailData="data.detailData" :inputConfig="inputConfig" :rules="rules" @pop="setdata" @changeData="changeData" ref="input"></g-details>
  </div>
</template>

<script>
import baseMixin from '@/mixins/BaseMixin'

export default {
  name: 'AsnDetails',
  mixins: [baseMixin],
  props: {
    data: {
      type: Object
    }
  },
  data() {
    return {
      rules: {
        receiptlinenumber: [
          { required: true, message: 'ASN/收货单不能为空', trigger: 'blur' }
        ],
        storerkey: [
          { required: true, message: '货主不能为空', trigger: 'blur' }
        ],
        sku: [{ required: true, validator: this.sku, trigger: 'blur' }],
        toloc: [{ required: true, validator: this.toloc, trigger: 'blur' }],
        packkey: [{ required: true, message: '包装不能为空', trigger: 'blur' }],
        uom: [{ required: true, message: '请选择计量单位', trigger: 'blur' }],
        // grosswgt: [ { required: true, validator: this.maxcount, trigger: "blur" }, ],
        // netwgt: [{ required: true, validator: this.maxcount, trigger: "blur" }],
        qtyexpected: [
          { required: true, validator: this.qtyexpected, trigger: 'blur' }
        ],
        qtyreceived: [
          { required: true, validator: this.qtyexpected, trigger: 'blur' }
        ]
      }
    }
  },
  computed: {
    inputConfig() {
      return [
        {
          type: 'input',
          label: this.$t('outbound.userlinenumber'),
          disabled: true,
          props: 'receiptlinenumber'
        },
        {
          type: 'select',
          label: this.$t('config.detailStatus'),
          disabled: true,
          props: 'status',
          options: this.getOptions('codelkup/RECSTATUS')
        },
        {
          type: 'table',
          label: this.$t('config.shipper'),
          disabled: true,
          props: 'storerkey',
          tableConfig: [
            {
              label: this.$t('config.shipper'),
              prop: 'storerkey',
              input: 'input'
            },
            {
              label: this.$t('config.company'),
              prop: 'company',
              input: 'input'
            },
            {
              label: this.$t('config.city'),
              prop: 'city',
              input: 'input'
            },
            { label: this.$t('config.provice'), prop: 'state', input: 'input' },
            {
              label: this.$t('config.postcode'),
              prop: 'zip',
              input: 'input'
            }
          ],
          url: `queryStorer/1/?/${this.$store.state.wms}`
        },
        {
          type: 'table',
          label: this.$t('config.sku'),
          disabled: !this.data.detailData.storerkey,
          props: 'sku',
          tableConfig: [
            {
              label: this.$t('config.sku'),
              prop: 'sku',
              input: 'input'
            },
            { label: this.$t('common.desc'), prop: 'descr', input: 'input' },
            {
              label: this.$t('config.shipper'),
              prop: 'storerkey',
              input: 'input'
            },
            {
              label: this.$t('config.packaging'),
              prop: 'packkey',
              input: 'input'
            },
            {
              label: this.$t('config.boxGroup'),
              prop: 'cartongroup',
              input: 'select',
              options: this.getOptions('queryCartonizationDropDown')
            },
            {
              label: this.$t('config.allocaStrategy'),
              prop: 'strategykey',
              input: 'select',
              options: this.getOptions('queryAllocatestrategyDropDown')
            },
            {
              label: this.$t('config.putawayStrategy'),
              prop: 'putawaystrategykey',
              input: 'select',
              disabled: false,
              options: this.getOptions('queryPutawaystrategyDropDown')
            },
            {
              label: this.$t('config.batchValidate'),
              prop: 'lottablevalidationkey',
              input: 'select',
              options: this.getOptions('queryLottablevalidationDropDown')
            },
            {
              label: this.$t('config.area'),
              prop: 'putawayzone',
              input: 'select',
              options: this.getOptions('queryPutawayzoneDropDown')
            }
          ],
          filterKey: ['descr', 'sku'],
          filterUrl: `fuzzyQuerySkuByItemNumberAndName/undefined`,
          url: `querySkuOfStorerkey/?/${this.$store.state.wms}/undefined`
        },
        {
          type: 'input',
          label: this.$t('config.goodsDesc'),
          disabled: true,
          props: 'descr'
        },
        {
          type: 'input',
          label: this.$t('config.goodsPackage'),
          disabled: true,
          props: 'packkey'
        },
        {
          type: 'select',
          label: this.$t('config.measureUnit'),
          disabled: false,
          props: 'uom',
          options: this.getOptions('codelkup/defaultUom/STD'),
          break: true
        },
        {
          type: 'input',
          label: this.$t('in.qtyexpected'),
          disabled: false,
          props: 'qtyexpected'
        },
        {
          type: 'input',
          label: this.$t('in.lpn'),
          disabled: true,
          props: 'toid'
        },
        {
          type: 'input',
          label: this.$t('in.qtyreceived'),
          props: 'qtyreceived',
          disabled: false
        },
        {
          type: 'input',
          label: this.$t('config.gross'),
          disabled: !this.$route.query.newFlag,
          props: 'grosswgt'
        },
        {
          type: 'input',
          label: this.$t('config.tareWeight'),
          disabled: true,
          props: 'tarewgt'
        },
        {
          type: 'input',
          label: this.$t('config.suttle'),
          disabled: !this.$route.query.newFlag,
          props: 'netwgt',
          break: true
        },

        {
          type: 'table',
          label: this.$t('config.loc'),
          disabled: true,
          props: 'toloc',
          tableConfig: [
            {
              label: this.$t('config.loc'),
              prop: 'loc',
              input: 'input'
            },
            {
              label: this.$t('common.type'),
              prop: 'locationtype',
              input: 'select',
              options: this.getOptions('querycodeLkup/LOCTYPE')
            },
            {
              label: this.$t('config.area'),
              prop: 'putawayzone',
              input: 'select',
              options: this.getOptions('queryPutawayzoneDropDown')
            },
            {
              label: this.$t('config.locFlag'),
              prop: 'locationflag',
              input: 'select',
              options: this.getOptions('querycodeLkup/LOCFLAG')
            },
            {
              label: this.$t('config.locCategory'),
              prop: 'locationcategory',
              input: 'select',
              options: this.getOptions('querycodeLkup/LOCCATEGRY')
            },
            {
              label: this.$t('config.loseId'),
              prop: 'loseid',
              input: 'select',
              options: this.getOptions('querycodeLkup/YESNO')
            },
            {
              label: this.$t('in.comminglelot'),
              prop: 'comminglelot',
              input: 'select',
              options: this.getOptions('querycodeLkup/YESNO')
            },
            {
              label: this.$t('config.commingleSku'),
              prop: 'comminglesku',
              input: 'select',
              options: this.getOptions('querycodeLkup/YESNO')
            },
            {
              label: this.$t('config.capacity'),
              prop: 'cubiccapacity',
              input: 'select',
              options: this.getOptions('querycodeLkup/YESNO')
            }
          ],
          url: 'queryLoc/'
        },
        {
          type: 'select',
          label: this.$t('in.qcrequired'),
          disabled: false,
          props: 'qcrequired',
          options: this.getOptions('codelkup/YESNO')
        },
        {
          type: 'select',
          label: this.$t('in.qcautoadjust'),
          disabled: false,
          props: 'qcautoadjust',
          options: this.getOptions('codelkup/YESNO')
        },
        {
          type: 'select',
          label: this.$t('config.freezeCode'),
          disabled: false,
          props: 'conditioncode',
          options: this.getOptions('codelkup/holdCode'),
          break: true
        },
        {
          type: 'date',
          label: this.$t('in.datereceived'),
          disabled: true,
          props: 'datereceived'
        },
        {
          type: 'date',
          label: this.$t('in.effectivedate'),
          disabled: false,
          props: 'effectivedate'
        },
        {
          type: 'input',
          label: this.$t('in.temperature'),
          disabled: false,
          props: 'temperature'
        },
        {
          type: 'input',
          label: 'PO/SO',
          disabled: true,
          props: 'pokey'
        },
        {
          type: 'input',
          label: this.$t('in.polinenumber'),
          disabled: false,
          props: 'polinenumber',
          break: true
        },
        {
          type: 'checkbox',
          label: this.$t('in.lotRequired'),
          disabled: true,
          props: ''
        },
        {
          type: 'checkbox',
          label: this.$t('in.wgtRequired'),
          disabled: true,
          width: '300px',
          props: ''
        }
      ]
    }
  },
  methods: {
    qtyexpected(rule, value, callback) {
      if (value === '') {
        callback()
      } else {
        var re = /^[0-9]*$/
        let te = value
        let flag = re.test(te)
        if (flag) {
          if (value < 0) {
            this.$message.error(this.$t('app.inputGreaterZeroNumberPlease'))
            this.data.detailData[rule.field] = ''
            callback(new Error())
          } else {
            callback()
          }
        } else {
          this.$message.error(this.$t('app.inputGreaterZeroNumberPlease'))
          this.data.detailData[rule.field] = ''
          callback(new Error())
        }
      }
    },
    maxcount(rule, value, callback) {
      if (value === '') {
        callback(new Error())
      } else {
        var re = /^[0-9]*$/
        let te = value
        let flag = re.test(te)
        if (flag) {
          if (value < 0) {
            this.$message.error(this.$t('app.inputGreaterZeroNumberPlease'))
            this.data.detailData[rule.field] = ''
            callback(new Error())
          } else {
            callback()
          }
        } else {
          this.$message.error(this.$t('app.inputGreaterZeroNumberPlease'))
          this.data.detailData[rule.field] = ''
          callback(new Error())
        }
      }
    },
    sku(rule, value, callback) {
      if (value === '') {
        callback(new Error())
      } else {
        this.$api
          .get(
            `/sku/isExist/${value}/${this.$store.state.wms}/${this.data.detailData.storerkey}`
          )
          .then(res => {
            
              if (res.bean) {
                const data = res.bean
                this.data.detailData.sku = data.sku
                this.data.detailData.packkey = data.packkey
                this.data.detailData.descr = data.descr
                
                callback()
            } else {
              this.$message.error(res.returnMgr)
              callback(new Error())
            }
          })
          .catch(e => {
            this.$message.error(e)
            callback(new Error())
          })
      }
    },
    toloc(rule, value, callback) {
      if (value === '') {
        callback(new Error())
      } else {
        this.$api
          .get(`queryLocCount/${value}`)
          .then(res => {
            
              if (parseInt(res.bean) > 0) {
                callback()
              } else {
                this.$message.error(this.$t('msg.msg23'))
                callback(new Error())
              }
          })
          .catch(e => {
            this.$message.error(e)
            callback(new Error())
          })
      }
    },
    async setdata(data, close, listId) {
      if (listId == 'storerkey') {
        this.data.detailData.storerkey = data.storerkey
      } else if (listId == 'sku') {
        this.data.detailData.sku = data.sku
        this.data.detailData.packkey = data.packkey
        this.data.detailData.descr = data.descr
        this.data.detailData.qcrequired = '0'
        this.data.detailData.conditioncode = ''
        if (data.rtxisexistscode == '0') {
          //无条码
          // this.data.detailData.toloc = "111111";
          let res = await this.$api.post(`/asn/detail/getToLocBySku`, data)
          
            this.data.detailData.toloc = res.bean.toloc
          
        } else {
          //有条码
          if (data.serialnumbe == '1') {
            this.data.detailData.toloc = 'QC'
            this.data.detailData.qcrequired = '1'
            this.data.detailData.conditioncode = 'QCREQ'
          } else {
            this.data.detailData.toloc = 'STAGE'
          }
        }
      } else if (listId == 'toloc') {
        this.data.detailData.toloc = data.loc
      }
      close(false)
    },
    changeData(data, prop, val, nodeData) {
      // if(prop == 'qcrequired'&&data.qcrequired == 1){
      //   this.data.detailData.qcrequired = "1";
      //   this.data.detailData.conditioncode = "QCREQ";
      //   this.data.detailData.toloc = "QC";
      // }
    }
  },
  mounted() {
    if (this.data.detailData.status === '0') {
      this.inputConfig.forEach((item, index) => {
        if (['toid', 'toloc'].includes(item.props)) {
          this.$set(this.inputConfig[index], 'disabled', false)
        }
      })
    }
  },
  watch: {
    '$parent.$parent.$refs.detail.detailData.storerkey': {
      handler(data) {
        this.data.detailData.storerkey = data
      },
      immediate: true,
      deep: true
    },
    // "data.detailData.qcrequired": {
    //   handler(data) {
    //     if(){
    //       this.inputConfig.forEach((item) => {
    //         if (item.props == "sku") {
    //           item.url = "querySkuOfStorerkey/?/wmwhse1/" + data;
    //         }
    //       });
    //     }
    //   },
    //   immediate: true,
    //   deep: true,
    // },
    'data.detailData.storerkey': {
      handler(data) {
        this.inputConfig.forEach(item => {
          if (item.props == 'sku') {
            item.filterUrl = 'fuzzyQuerySkuByItemNumberAndName/' + data
            item.url = 'querySkuOfStorerkey/?/wmwhse1/' + data
          }
        })
      },
      immediate: true,
      deep: true
    },
    data: {
      handler(newValue, oldValue) {
        if (newValue.detailData.status === '0') {
          this.inputConfig.forEach((item, index) => {
            if (['toid', 'toloc'].includes(item.props)) {
              this.$set(this.inputConfig[index], 'disabled', false)
            }
          })
        }
        this.$store.commit('setTemporary', newValue)
      },
      deep: true
    }
  }
}
</script>

<style scoped></style>
